package exchange.fulltest;

import static org.junit.Assert.*;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import exchange.netserver.Server;
import exchange.util.Exchange;
import exchange.util.InfoService;
import exchange.util.Stock;

public class Fulltest {

	private static final int NTHREDS = 10;
	private ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);

	@Test
	public void shouldStayNineOrdersWhenMatchingIsFinished()
			throws InterruptedException {
		// given
		Stock GOLD = new Stock("GOLD");
		Stock WOOD = new Stock("WOOD");
		Stock ORE = new Stock("ORE");
		Stock GEMS = new Stock("GEMS");
		Stock CRYSTALS = new Stock("CRYSTALS");
		Stock SULFUR = new Stock("SULFUR");
		Stock MERCURY = new Stock("MERCURY");

		Exchange.addNewStock(GOLD);
		Exchange.addNewStock(WOOD);
		Exchange.addNewStock(ORE);
		Exchange.addNewStock(GEMS);
		Exchange.addNewStock(CRYSTALS);
		Exchange.addNewStock(SULFUR);
		Exchange.addNewStock(MERCURY);

		Executors.newSingleThreadExecutor().execute(new Server());

		// when
		executor.execute(new NetClient("test999", "ASK;GOLD;99.9", 10));
		for (int i = 0; i < 10; i++) {
			executor.execute(new NetClient("test1" + i, "ASK;GOLD;100", 10));
			executor.execute(new NetClient("test2" + i, "BID;GOLD;100", 10));
		}

		executor.shutdown();
		executor.awaitTermination(1000, TimeUnit.SECONDS);
		
		// then

		Thread.sleep(1000);
		
		InfoService.printOrderBook(GOLD);
		assertEquals(10, GOLD.getAskList().size(), 0.001);
		assertEquals( 0, GOLD.getBidList().size(), 0.001);
		assertEquals(99.9, GOLD.getAskList().get(0).getPrice(),  0.001);
		assertEquals(	1, GOLD.getAskList().get(0).getAmount(), 0.001);
	}

}
